home *** CD-ROM | disk | FTP | other *** search
/ Gekkan Dennou Club 142 / Gekkan Dennou Club - 2000.3 Vol. 142 (Japan).7z / Gekkan Dennou Club - 2000.3 Vol. 142 (Japan) (Track 1).bin / tools / pws / pws011.lzh / PersonalWS.c < prev    next >
C/C++ Source or Header  |  2000-02-01  |  4KB  |  188 lines

  1. /* PersonelWS.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <sys/xglob.h>
  7. #include <sys/dos.h>
  8.  
  9. #define GLOBAL_DEFINE        /* グローバル変数を確保する */
  10. #include "PersonalWS.h"
  11. #include "ServerCore.h"
  12.  
  13. #define MY_USER_STACK    48*1024
  14. #define MY_SUPER_STACK    32*1024
  15.  
  16.  
  17.  
  18. /* 偶数アドレスに整合するためにわざわざ short で宣言している */
  19. /* char とかで宣言するとスタックの底が奇数アドレスに設定されたりされなかったり */
  20. static short user_stack[MY_USER_STACK / 2];
  21. static short super_stack[MY_SUPER_STACK / 2];
  22.  
  23. static char *id_str = "PersonalWS";    /* 常駐識別用文字列 */
  24. static struct _prcctrl buff;
  25. static int v_option = 0;
  26.  
  27. /* スタックサイズとヒープサイズを指定 */
  28. int _stacksize = 32 * 1024;
  29. int _heapsize = 256 * 1024;
  30.  
  31.  
  32.  
  33. static void usage (void)
  34. {
  35.     printf ("パーソナルウェブサーバー PersonalWS.x ver0.11\n"
  36.         "        programmed by Mitsuky <FreeSoftware>\n"
  37.         "ウェブサーバーです\n"
  38.         "使用法 : PersonalWS [option] [ベースディレクトリ]\n"
  39.         "[option]\n"
  40.         "    -V : 詳細表示モード\n"
  41.         "    -Q文字列 : 終了文字列(無指定時は QUIT )\n"
  42.         "    -R : 常駐解除\n"
  43.         "ベースディレクトリを省略するとカレントディレクトリをベースとします\n"
  44.         "BGプロセスで動作するので Human68K ver.2 以降、\n"
  45.         "かつ CONFIG.SYS で PROCESS = の設定をする必要があります\n"
  46.         );
  47. }
  48.  
  49.  
  50. /* おしゃべりモード用の printf() (要 stdarg.h) */
  51. int verbose_printf (const char *format,...)
  52. {
  53.     if (v_option) {
  54.         va_list ap;
  55.         va_start (ap, format);
  56.         vprintf (format, ap);
  57.         va_end (ap);
  58.     }
  59.     return (0);
  60. }
  61.  
  62.  
  63. /* 常駐部 */
  64. static void keepent (void)
  65. {
  66.     for (;;) {
  67.         if (buff.your_id != 0xffff) {
  68.             /* スレッド間通信があった場合 */
  69.             int th_command;
  70.  
  71.             th_command = buff.command;
  72.             buff.your_id = 0xffff;    /* 送り手のスレッドIDを -1 に */
  73.  
  74.             if (th_command == 0xfff9) {    /* 自殺コマンド? */
  75.                 ServerTini ();
  76.                 _dos_kill_pr ();
  77.             }
  78.         } else {
  79.             /* スレッド間通信がなかった場合 */
  80.             if (ServerMain () < 0) {
  81.                 ServerTini ();
  82.                 _dos_kill_pr ();
  83.             }
  84.             _dos_change_pr ();
  85.         }
  86.     }
  87. }
  88.  
  89.  
  90. int main (int argc, char *argv[])
  91. {
  92.     int i;
  93.     int slash_flag = 0;
  94.     struct _prcptr prc;
  95.     int my_id, your_id;
  96.     int r_option = 0;
  97.  
  98.     strcpy (base_dir, "./");    /* 無指定時はカレントディレクトリ */
  99.     quit_str = "QUIT";
  100.  
  101.     {
  102.         char *temp;
  103.  
  104.         temp = getenv ("SLASH");
  105.         if ((temp != NULL) && (*temp == '/')) {
  106.             slash_flag = 1;
  107.         }
  108.     }
  109.  
  110.     for (i = 1; i < argc; i++) {
  111.         if (('-' == *argv[i]) || ((slash_flag == 0) && ('/' == *argv[i]))) {
  112.             switch (*(argv[i] + 1)) {
  113.  
  114.             case 'r':
  115.             case 'R':
  116.                 r_option = !0;
  117.                 break;
  118.  
  119.             case 'v':
  120.             case 'V':
  121.                 v_option = !0;
  122.                 break;
  123.  
  124.             case 'q':
  125.             case 'Q':
  126.                 quit_str = argv[i] + 2;
  127.                 break;
  128.  
  129.             default:
  130.                 usage ();
  131.                 return (-1);
  132.             }
  133.         } else {
  134.             strcpy (base_dir, argv[i]);
  135.             _addlastsep (base_dir);
  136.             _toslash (base_dir);
  137.         }
  138.     }
  139.  
  140.     my_id = _dos_get_pr (-2, &prc);
  141.     strcpy (prc.name, id_str);
  142.     your_id = _dos_get_pr (-1, &prc);
  143.     if (your_id < 0) {    /* 常駐しているか? */
  144.         if (!r_option) {
  145.             /* 常駐していない場合は常駐 */
  146.             struct _psp *my_psp;
  147.             void *mem_top, *mem_last;
  148.  
  149.             if (ServerInit ())
  150.                 return (-1);
  151.             printf ("常駐します\n");
  152.             buff.length = 0;
  153.             buff.command = 0xffff;
  154.             buff.your_id = -1;
  155.             _dos_open_pr (id_str, 2, (int) &user_stack[MY_USER_STACK / 2], (int) &super_stack[MY_SUPER_STACK / 2], 0, (int) &keepent, &buff, 1);
  156.             my_psp = _dos_getpdb ();
  157.             mem_top = (int *) ((int) my_psp - 0x10);
  158.             mem_last = (int *) (*(int *) ((int) mem_top + 0x08));
  159.             _dos_keeppr ((int) mem_last - (int) mem_top - (0x10 + 0xf0), 0);
  160.         } else {
  161.             printf ("まだ常駐していません\n");
  162.             return (-1);
  163.         }
  164.     } else {
  165.         if (r_option) {
  166. #if    0
  167.             /* 既に常駐していた場合は自殺コマンドを送る */
  168.             ServerTini ();
  169.             while (_dos_send_pr (my_id, your_id, 0xfff9, NULL, 0) == -28)
  170.                 _dos_change_pr ();
  171. #endif
  172.             int temp_sock;
  173.             char temp_str[256];
  174.             temp_sock = OpenSock ("localhost", 80);
  175.             sprintf (temp_str, "GET /%s HTTP/1.0\r\n\r\n", quit_str);
  176.             SendCommand (temp_sock, temp_str);
  177.             ReceiveMessage (temp_sock);
  178.             CloseSock (temp_sock);
  179.  
  180.             printf ("常駐解除しました\n");
  181.         } else {
  182.             printf ("既に常駐しています\n");
  183.             return (-1);
  184.         }
  185.     }
  186.     return (0);
  187. }
  188.